﻿Imports Microsoft.Practices.Composite.Regions
Imports Microsoft.Practices.Composite.Events
Imports Microsoft.Practices.Composite.Wpf.Commands
Imports Hinshelwood.TFSStickyBuddy.Modules.Interfaces
Imports Microsoft.Practices.Unity
Imports Hinshelwood.TFSStickyBuddy.Infrastructure
Imports Hinshelwood.TFSStickyBuddy.Infrastructure.Events

Namespace Controllers


    Public Class LogInController
        Implements ILogInController


        'Private m_IDMgr As IdentityManager
        'Private m_ID As Identity

        Private ReadOnly m_regionManager As IRegionManager
        Private ReadOnly m_eventAggregator As IEventAggregator
        Private ReadOnly m_container As IUnityContainer
        Private Shared m_SignInCommand As DelegateCommand(Of Uri)
        Private Shared m_SignOutCommand As DelegateCommand(Of Uri)

        Public Sub New(ByVal regionManager As IRegionManager, ByVal container As IUnityContainer, ByVal eventAggregator As IEventAggregator)
            m_regionManager = regionManager
            m_eventAggregator = eventAggregator
            m_container = container
            m_SignInCommand = New DelegateCommand(Of Uri)(AddressOf OnSignInExecuted, AddressOf OnSignInCanExecute)
            m_SignOutCommand = New DelegateCommand(Of Uri)(AddressOf OnSignOutExecuted, AddressOf OnSignOutCanExecute)
            'Try
            '    m_IDMgr = IdentityManager.CreateInstance("000000004C002239", "TFS Sticky Buddy")
            'Catch ex As WLLogOnException
            '    'Check to see if FlowUrl is defined.
            '    If Not ex.FlowUrl = Nothing Then
            '        'If FlowUrl is defined, direct user to the web page to correct the error.
            '        MessageBox.Show(ex.ErrorString + "Please go to " + ex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error")
            '    Else
            '        'If FlowUrl is not defined, simply display the ErrorString.
            '        MessageBox.Show(ex.ErrorString)
            '    End If
            'End Try

            'Check the config file to see if a default user is defined.

            'If no default user is defined, try instantiating the global Identity object instance from scratch.
            'Try
            '    m_ID = m_IDMgr.CreateIdentity()
            'Catch ex As WLLogOnException
            '    'Check to see if FlowUrl is defined.
            '    If Not ex.FlowUrl = Nothing Then
            '        'If FlowUrl is defined, direct user to the web page to correct the error.
            '        MessageBox.Show(ex.ErrorString + "Please go to " + ex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error")
            '    Else
            '        'If FlowUrl is not defined, simply display the ErrorString.
            '        MessageBox.Show(ex.ErrorString)
            '    End If
            'End Try
        End Sub

        Public ReadOnly Property SignInCommand() As Microsoft.Practices.Composite.Wpf.Commands.DelegateCommand(Of System.Uri) Implements Interfaces.ILogInController.SignInCommand
            Get
                Return m_SignInCommand
            End Get
        End Property

        Public ReadOnly Property SignOutCommand() As Microsoft.Practices.Composite.Wpf.Commands.DelegateCommand(Of System.Uri) Implements Interfaces.ILogInController.SignOutCommand
            Get
                Return m_SignOutCommand
            End Get
        End Property

        Sub OnSignInExecuted(ByVal server As Uri)
            '----------------------------
            m_eventAggregator.GetEvent(Of Events.NotImplementedEvent)().Publish(New Events.NotImplementedPayload("Sign In", Me.GetType, "n/a"))
            'Try
            '    'Try to authenticate the user by showing the Sign-In dialog window.
            '    If m_ID.Authenticate() Then
            '        'currentUserName = oID.UserName;
            '    Else
            '        MessageBox.Show("Authentication failed")
            '    End If
            '    SignOutCommand.RaiseCanExecuteChanged()
            '    SignInCommand.RaiseCanExecuteChanged()
            'Catch ex As WLLogOnException
            '    'Check to see if FlowUrl is defined.
            '    If Not ex.FlowUrl = Nothing Then
            '        'If FlowUrl is defined, direct user to the web page to correct the error.
            '        MessageBox.Show(ex.ErrorString + "Please go to " + ex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error")
            '    Else
            '        'If FlowUrl is not defined, simply display the ErrorString.
            '        MessageBox.Show(ex.ErrorString)
            '    End If
            'End Try
        End Sub

        Sub OnSignOutExecuted(ByVal server As Uri)
            '------------------------------------------
            m_eventAggregator.GetEvent(Of Events.NotImplementedEvent)().Publish(New Events.NotImplementedPayload("Sign Out", Me.GetType, "n/a"))
            ''If user is authenticated, they intended to Sign-Out. Try signing the user out.
            'Try
            '    m_ID.CloseIdentityHandle()
            '    SignOutCommand.RaiseCanExecuteChanged()
            '    SignInCommand.RaiseCanExecuteChanged()
            'Catch ex As WLLogOnException
            '    'Check to see if FlowUrl is defined.
            '    If Not ex.FlowUrl = Nothing Then
            '        'If FlowUrl is defined, direct user to the web page to correct the error.
            '        MessageBox.Show(ex.ErrorString + "Please go to " + ex.FlowUrl.AbsoluteUri + "to correct the condition that caused the error")
            '    Else
            '        'If FlowUrl is not defined, simply display the ErrorString.
            '        MessageBox.Show(ex.ErrorString)
            '    End If
            'End Try
        End Sub

        Function OnSignInCanExecute(ByVal server As Uri) As Boolean
            Return True 'Not m_ID Is Nothing AndAlso Not m_ID.IsAuthenticated
        End Function

        Function OnSignOutCanExecute(ByVal server As Uri) As Boolean
            Return False 'Not m_ID Is Nothing AndAlso m_ID.IsAuthenticated
        End Function

        Public Sub TrySilentSignIn() Implements Interfaces.ILogInController.TrySilentSignIn

        End Sub

    End Class

End Namespace